package com.gmail.genzouw.gconv

import groovy.util.GroovyTestCase

class GconvTest extends GroovyTestCase {
	def gconv = null

	void setUp(){
	    gconv = Gconv.instance
	}

	void test_guess_utf8(){
		def utf8bytes = new File("src/test/resources/txt/utf8.txt").bytes
		assertEquals(Gconv.Charset.UTF_8, gconv.guess(utf8bytes))
	}
	void test_guess_euc(){
		def eucbytes = new File("src/test/resources/txt/euc.txt").bytes
		assertEquals(Gconv.Charset.EUC_JP, gconv.guess(eucbytes))
	}
	void test_guess_sjis(){
		def sjisbytes = new File("src/test/resources/txt/shiftjis.txt").bytes
		assertEquals(Gconv.Charset.SHIFT_JIS, gconv.guess(sjisbytes))
	}

	void test_isUtf8(){
		def bytes = new File("src/test/resources/txt/utf8.txt").bytes
		assertTrue(gconv.isUtf8(bytes))
		assertFalse(gconv.isEuc(bytes))
		assertFalse(gconv.isSjis(bytes))
	}
	void test_isEuc(){
		def bytes = new File("src/test/resources/txt/euc.txt").bytes
		assertFalse(gconv.isUtf8(bytes))
		assertTrue(gconv.isEuc(bytes))
		assertFalse(gconv.isSjis(bytes))
	}
	void test_isSjis(){
		def bytes = new File("src/test/resources/txt/shiftjis.txt").bytes
		assertFalse(gconv.isUtf8(bytes))
		assertFalse(gconv.isEuc(bytes))
		assertTrue(gconv.isSjis(bytes))
	}

	void test_fromEucToSjis_size(){
		def sjisbytes = new File("src/test/resources/txt/shiftjis.txt").bytes
		def eucbytes = new File("src/test/resources/txt/euc.txt").bytes
		assertEquals(sjisbytes, gconv.toSjis(eucbytes))
	}

	void test_resourcesValid(){
		def sjisbytes = new File("src/test/resources/txt/shiftjis.txt").bytes.toList()
		def eucbytes = new File("src/test/resources/txt/euc.txt").bytes.toList()
		def utf8bytes = new File("src/test/resources/txt/utf8.txt").bytes.toList()
		def utf8nbytes = new File("src/test/resources/txt/utf8n.txt").bytes.toList()
		assertFalse(sjisbytes == eucbytes)
		assertFalse(sjisbytes == utf8bytes)
		assertFalse(sjisbytes == utf8nbytes)
		assertFalse(utf8nbytes == eucbytes)
		assertFalse(utf8nbytes == utf8bytes)
		assertFalse(eucbytes == utf8bytes)
	}
}
